package cn.ococci.dm.Main.Activity;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import cn.ococci.dm.Base.BaseActivity;
import cn.ococci.dm.Main.Const;
import cn.ococci.dm.Main.Tools.LogTool;
import cn.ococci.dm.Main.Tools.SpTool;
import cn.ococci.dm.Main.Tools.StringTool;
import cn.ococci.dm.Main.Tools.ToastTool;
import cn.ococci.dm.Main.UserBiz;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/12/29.
 */
public class LoginActivity extends BaseActivity {


    private LinearLayout loginLy;

    private EditText mobileNumber;
    private EditText password;
    private Button sign_in_button;

    @Override
    protected int getResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

        mobileNumber = (EditText)findViewById(R.id.mobileNumber);
        password = (EditText) findViewById(R.id.password);
        sign_in_button = (Button) findViewById(R.id.sign_in_button);

        loginLy = (LinearLayout) findViewById(R.id.loginLy);

    }

    @Override
    protected void initData() {

        String phone = SpTool.getString(Const.LoginParams.USER_ACCOUNT,"");
        mobileNumber.setText(phone);
        mobileNumber.setSelection(phone.length());

    }

    @Override
    protected void initListener() {

        sign_in_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });


        loginLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogTool.log(LogTool.CYAN,"点击了");
                hideInput();
            }
        });


    }

    public synchronized  void hideInput() { // 不显示键盘
        View v = this.getCurrentFocus();
        if (v != null && v instanceof EditText) {
            InputMethodManager imm = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 强制隐藏键盘
        }
    }


    private void signIn() {

        String mobile = mobileNumber.getText().toString().trim();

        if (StringTool.isEmpty(mobile)) {
            ToastTool.show(R.string.login_input_account);
            return;
        }

        if (!StringTool.isPhone(mobile)) {
            ToastTool.show(R.string.login_check_phone_error);
            return;
        }

        String pwd = password.getText().toString().trim();


        if (StringTool.isEmpty(pwd)) {
            ToastTool.show(R.string.login_pwd_null);
            return;
        }

        if (StringTool.isPwdToShort(pwd)) {
            ToastTool.show(R.string.login_pwd_short);
            return;
        }

        UserBiz.signIn(this,mobile,pwd);
    }

}
